package com.example.mybatisplusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.CustomIdGenerator;
import com.example.mybatisplusdemo.entity.Student;
import com.example.mybatisplusdemo.service.StudentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
@Slf4j
@AllArgsConstructor
public class StudentController {

    private StudentService studentService;

    @GetMapping("/{id}")
    public Student get(@PathVariable String id) {
        return studentService.getById(id);
    }

    @GetMapping("/listAll")
    public List<Student> listAll() {
        return studentService.list();
    }

    @PostMapping
    public void save(@RequestBody Student student) {
        studentService.save(student);
    }

    @PostMapping("/saveBatch")
    public void saveBatch(@RequestBody List<Student> list) {
        studentService.saveBatch(list);
    }

    @PostMapping("/saveBatch/{num}")
    public void saveBatches(@RequestBody List<Student> list, @PathVariable int num) {
        studentService.saveBatch(list, num);
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody Student student) {
        studentService.saveOrUpdate(student);
    }

    @PostMapping("/saveBatchByWrapper")
    public void saveBatchByWrapper(@RequestBody Student student) {
        Wrapper<Student> wrapper = new QueryWrapper<>();
        studentService.saveOrUpdate(student, wrapper);
    }

    @PostMapping("/saveOrUpdateBatch")
    public void saveOrUpdateBatch(@RequestBody List<Student> list) {
        studentService.saveOrUpdateBatch(list);
    }

    @PostMapping("/saveOrUpdateBatches")
    public void saveOrUpdateBatches(@RequestBody List<Student> list, @PathVariable int num) {
        studentService.saveBatch(list, num);
    }

    @GetMapping("/page")
    public Page<Student> page(@RequestBody Student student) {
        Page<Student> page = new Page<>(1, 5);
        return studentService.page(page);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        studentService.removeById(id);
    }


    @GetMapping("/pageByWrapper")
    public Page<Student> pageByWrapper(@RequestBody Student student) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like("name", "a");
        Page<Student> page = new Page<>(1, 5);
        return studentService.page(page,wrapper);
    }
}
